# encoding: utf-8
# Author    : limusen
# Datetime  : 2021/12/6 7:54 下午


import os
from common.excel_utils import ExcelUtils

current = os.path.dirname(os.path.abspath(__file__))
excel_file_path = os.path.join(current, '..', 'testcase_data', 'testcase_infos.xlsx')
excel_sheet_name = "Sheet1"


class CasesDataUtils:

    def __init__(self):
        self.excel_data = ExcelUtils(excel_file_path=excel_file_path,
                                     sheet_name=excel_sheet_name)

    def convert_testcase_data_to_dict(self):
        """
        转换成列表包夹字典格式
        [{字段名1：字段值1}，{字段名1：字段值1}，{字段名1：字段值1}]
        抓换之后
        {"字段值1":[{字段名1：字段值1},{字段名1：字段值1},{字段名1：字段值1}]}
        :return:
        """
        testcase_data_dict = {}
        for row_data in self.excel_data.get_all_excel_data():
            if row_data["用例执行"] == "是":
                testcase_data_dict.setdefault(row_data['测试用例编号'], []).append(row_data)
        return testcase_data_dict

    def convert_testcases_data_to_list(self):
        """
        将列表转换成列表包夹字典
        :return:
        """
        testcase_data_list = []
        for key, value in self.convert_testcase_data_to_dict().items():
            type_dict = {}
            type_dict["case_id"] = key
            type_dict["case_step"] = value
            testcase_data_list.append(type_dict)
        return testcase_data_list


if __name__ == '__main__':
    lista = CasesDataUtils().convert_testcases_data_to_list()
    print(lista)
    # for i in lista:
    #     print(i)
